package cn.fl.continfo.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.commons.result.ResponseData;
import cn.fl.continfo.service.IPrjContCfgService;
import cn.fl.continfo.vo.PrjContCfgVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.IPrjContCfgApiService;
import cn.fl.preloan.condition.PrjContCfgQueryCondition;
import cn.fl.preloan.dto.PageResponseDTO;
import cn.fl.preloan.dto.PrjContCfgDTO;
import cn.fl.preloan.dto.PrjPrdVehInfoDTO;
import cn.fl.preloan.dto.PrjPrdVehInfoPageResponseDTO;
import cn.fl.project.dto.PrjPrdGpsApplyDTO;
import flow.business.facade.flow.condition.SysFlowSHQueryCondition;
import flow.business.facade.flow.dto.SysFlowSHDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class PrjContCfgServiceImpl implements IPrjContCfgService {

    @Autowired
    private IPrjContCfgApiService prjContCfgApiService;

    @Override
    public PrjContCfgVO selectById(Long id) throws BaseException {
        if(null == id){
            throw new BaseException("查询合同方案id为空");
        }
        ResponseData<PrjContCfgDTO> responseData = prjContCfgApiService.selectById(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentBuOrgCd());
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            PrjContCfgDTO dto = responseData.getData();
            PrjContCfgVO vo = new PrjContCfgVO();
            BeanUtils.copyProperties(dto,vo);
            return vo;
        }
        return null;
    }

    @Override
    public Long add(PrjContCfgVO entity) throws BaseException {

        if(null == entity){
            throw new BaseException("新增合同方案实体为空");
        }
        PrjContCfgDTO dto = new PrjContCfgDTO();
        BeanUtils.copyProperties(entity,dto);
        //新增赋默认值
        Date date = new Date();
        dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        dto.setCrtTm(date);
        dto.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        dto.setMdfTm(date);
        System.out.println(JSON.toJSON(dto));
        ResponseData<Long> responseData = prjContCfgApiService.add(dto);
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            return responseData.getData();
        }else{
            throw new BaseException(responseData.getMessage());
        }
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if(null == id){
            throw new BaseException("删除合同方案id为空");
        }
        ResponseData responseData = prjContCfgApiService.removeById(id);
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            return true;
        }
        return false;
    }

    @Override
    public boolean modify(PrjContCfgVO entity) throws BaseException {
        if(null == entity){
            throw new BaseException("修改合同方案实体为空");
        }
        PrjContCfgDTO dto = new PrjContCfgDTO();
        BeanUtils.copyProperties(entity,dto);
        ResponseData responseData = prjContCfgApiService.modify(dto);
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            return true;
        }else{
            throw new BaseException(responseData.getMessage());
        }
    }

    @Override
    public Pager searchListPage(int pageSize, int pageNum, PrjContCfgQueryCondition condition) throws BaseException {
        if(pageSize<1 || pageNum<0){
            throw new BaseException("分页查询合同方案参数异常");
        }
        ResponseData<PageResponseDTO<PrjContCfgDTO>> responseData = prjContCfgApiService.selectListPage(pageSize,pageNum,condition);

        Pager pager = new Pager();
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            PageResponseDTO pageDto = responseData.getData();
            if(null!=pageDto && CollectionUtils.isNotEmpty(pageDto.getList())){
                List<PrjContCfgVO> list = new ArrayList<>();
                for(Object dto : pageDto.getList()){
                    PrjContCfgVO vo = new PrjContCfgVO();
                    BeanUtils.copyProperties(dto,vo);
                    list.add(vo);
                }
                pager.setTotalCount(pageDto.getTotalCount());
                pager.setResultList(list);
            }
        }
        return pager;
    }


}
